package com.yanqu.road.server.channelserver.manager.activity.tonbing.logic;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.channelserver.manager.activity.tonbing.ChannelTonBingMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 统兵演武聊天
 */
public class ChannelTonBingChatLogic {

    public static final int MAX_MESSAGE_COUNT = 50;

    public static final int RANGE_ALL = 1;      //所有的
    public static final int RANGE_CAMP = 2;     //阵营的
    public static final int RANGE_BATTLEFIELD = 3;  //战场的
    public static final int RANGE_MATCH = 4;  //比赛的

    private ActivityInfo activityInfo;

    public ChannelTonBingChatLogic(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    /**
     * 范围，ID（活动、阵营、战场），消息类型（系统、玩家），内容
     */
    private Map<Integer, Map<Integer, Map<Integer, WorldMessageProto.WorldMessageListMsg.Builder>>> chatMapMap = new ConcurrentHashMap<>();

    public void clearChat(){
        chatMapMap = new ConcurrentHashMap<>();
    }

    /**
     * 发聊天
     *
     * @param range 范围：1全部，2阵营，3战场
     * @param type 类型 0 系统消息， 1:玩家消息
     */
    public void chat(int range, int id, int type, WorldMessageProto.WorldMessageTempMsg chatMsg,int matchId) {
        synchronized (chatMapMap) {
            chatMapMap.putIfAbsent(range, new ConcurrentHashMap<>());
            chatMapMap.get(range).putIfAbsent(id, new ConcurrentHashMap<>());
            chatMapMap.get(range).get(id).putIfAbsent(type, WorldMessageProto.WorldMessageListMsg.newBuilder());
            WorldMessageProto.WorldMessageListMsg.Builder chatListMsg = chatMapMap.get(range).get(id).get(type);
            if (chatListMsg.getDataListCount() >= MAX_MESSAGE_COUNT) {
                chatListMsg.removeDataList(0);
            }
            chatListMsg.addDataList(chatMsg);
        }
        ChannelTonBingMgr.getActivity(activityInfo.getActivityId()).syncChat(range, id, chatMsg,matchId);
    }

    /**
     * 看聊天
     */
    public List<WorldMessageProto.WorldMessageTempMsg> getChatMsg(int range, int id) {
        List<WorldMessageProto.WorldMessageTempMsg> dataList = new ArrayList<>();
        if (chatMapMap.containsKey(range)) {
            if (chatMapMap.get(range).containsKey(id)) {
                Map<Integer, WorldMessageProto.WorldMessageListMsg.Builder> map = chatMapMap.get(range).get(id);
                for (WorldMessageProto.WorldMessageListMsg.Builder value : map.values()) {
                    dataList.addAll(value.getDataListList());
                }
            }
        }
        return dataList;
    }

    /**
     * 清空聊天
     */
    public void clearBattlefieldChat(int cityId) {
        Map<Integer, Map<Integer, WorldMessageProto.WorldMessageListMsg.Builder>> map = chatMapMap.get(RANGE_BATTLEFIELD);
        if (map != null) {
            Map<Integer, WorldMessageProto.WorldMessageListMsg.Builder> dataMap = map.get(cityId);
            if(null != dataMap) {
                for (WorldMessageProto.WorldMessageListMsg.Builder builder : dataMap.values()) {
                    if (builder != null) {
                        builder.clear();
                    }
                }
            }
        }
    }

}
